/*
 * create:2016年5月2日 上午9:48:33
 * author:wangds  
 */
package com.cxkj.shop.biz.card;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.cxkj.shop.biz.AbstractBaseDeliveryHttpInteractionBusiness;
import com.cxkj.shop.biz.pay.wx.WxPayBusiness;
import com.cxkj.shop.model.card.Card;
import com.cxkj.shop.model.order.DefaultOrder;
import com.cxkj.shop.model.order.DefaultOrderItem;
import com.cxkj.shop.model.order.Order;
import com.cxkj.shop.model.order.OrderItem;
import com.cxkj.shop.model.shoppingcart.CommodityItem;
import com.cxkj.shop.proc.AbstractWithResMybatisProc;
import com.cxkj.shop.utils.BusinessUtils;

import net.wangds.common.context.BeanContext;
import net.wangds.comp.json.JsonUtil;
import net.wangds.utilities.lang.StringUtils;

/**
 * .
 * 
 * @author wangds
 *
 */
public class PayForACardBusiness extends AbstractBaseDeliveryHttpInteractionBusiness<Map<String, Object>, Map<String, Object>> {

  private AbstractWithResMybatisProc<Order, Integer> orderProc = null;

  /*
   * @see
   * net.wangds.comp.business.http.AbstractBaseHttpInteractionBusiness#process(
   * java.lang.Object)
   * 
   * @author wangds 2016年5月2日 上午9:50:25.
   */
  /**
   * The getter method of the property orderProc.
   * 
   * @author wangds 2016年5月2日 上午10:17:25.
   * @return the orderProc.
   */
  public AbstractWithResMybatisProc<Order, Integer> getOrderProc() {
    return orderProc;
  }

  /**
   * The setter method of the property orderProc.
   * 
   * @param theorderProc
   *          the orderProc to set
   * @author wangds 2016年5月2日 上午10:17:25.
   */
  public void setOrderProc(AbstractWithResMybatisProc<Order, Integer> orderProc) {
    this.orderProc = orderProc;
  }

  @Override
  protected Map<String, Object> processBiz(Map<String, Object> map) throws Exception {
    Map<String, Object> res = Collections.emptyMap();
    
    this.getLogger().debug("----------------------------\n\n\n"+JsonUtil.toJson(map));
    
    Order order = createOrder(((Number) map.get("cardType")).intValue());
    fillOrderPostAddress(order);
    this.orderProc.setParameter(order);
    this.orderProc.process();

    if (this.orderProc.getResult() >= 0) {
      res = pay(order);
    }else{
      throw new Exception();
    }

    return res;
  }

  /**
   * .
   * 
   * @author wangds 2016年5月2日 上午10:57:22.
   * @param order
   * @return
   */
  protected Map<String, Object> pay(Order order) {
    BeanContext ctx = BusinessUtils.getContext();

    Map<String, Object> order_info_map = new HashMap<>();
    order_info_map.put("orderId", order.getOrderId());

    WxPayBusiness wxpay = ctx.getBean("biz_wx_pay");
    wxpay.setHttpRequest(this.getHttpRequest());
    wxpay.setHttpResponse(this.getHttpResponse());
    wxpay.setInput(order_info_map);
    wxpay.process();
    return wxpay.getOutput();
  }

  /**
   * .
   * 
   * @author wangds 2016年4月15日 下午1:44:16.
   * @param form_address
   * @param order
   */
  protected void fillOrderPostAddress(Order order) {
    order.setAddress("虚拟卡");
    order.setPhone("");
    order.setPostCode("000000");
    order.setReceiverName("");
  }

  /**
   * .
   * 
   * @author wangds 2016年4月15日 下午1:29:44.
   * @return
   */
  @SuppressWarnings("unchecked")
  protected Order createOrder(int cardType) {
    
    if(cardType<0||cardType>4){
      throw new RuntimeException(StringUtils.join("The card type must be one of 1-4，but it is \"",cardType,"\""));
    }
    
    DefaultOrder res = new DefaultOrder();
    HttpSession ssn = BusinessUtils.getHttpSessionFromHttpBusiness(this);
    BeanContext c = BusinessUtils.getContext();
    
    
    Card card = c.getBean("card_type_"+cardType);
    
    res.setOrderId(BusinessUtils.newOrderId());
    res.setOpenId(BusinessUtils.getOpenIdSession(ssn));
    res.setCreateTm(System.currentTimeMillis());

    int total = 0;
    int total_score = 0;
    List<OrderItem> items = new LinkedList<OrderItem>();
    DefaultOrderItem oitem = new DefaultOrderItem();
    oitem.setOrderId(res.getOrderId());
    oitem.setCommodityId(card.getId());
    oitem.setCommodityName(card.getName());
    oitem.setCommodityCode("");
    oitem.setPrice(card.getPrice());
    oitem.setAmount(1);
    oitem.setScore(0);
    oitem.setGprice(0);
    oitem.setGscore(0);
    oitem.setLprice(0);
    oitem.setLscore(0);
    oitem.setGrouppayId(0);
    oitem.setTimelimitpayId(0);
    oitem.setType(CommodityItem.TYPE_CARD);
    total += (oitem.getPrice() * oitem.getAmount());
    total_score += (oitem.getScore() * oitem.getAmount());
    items.add(oitem);

    res.setItems(items);
    res.setTotal(total);
    res.setTotalScore(total_score);
    return res;
  }

}
